package com.apache.audit.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.audit.entity.LogOperationDetail;
import com.apache.audit.manager.LogOperationDetailManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class LogOperationDetailManagerImpl implements LogOperationDetailManager {

	protected IDao logOperationDetailDao;
	protected final String entityName = "com.apache.audit.entity.LogOperationDetail";

	public void setLogOperationDetailDao(IDao logOperationDetailDao) {
		this.logOperationDetailDao = logOperationDetailDao;
	}

	public String saveInfo(ParamsVo<LogOperationDetail> vo) throws BusinessException {
		LogOperationDetail logOperationDetail = vo.getObj();
		if (Validator.isEmpty(logOperationDetail)) {//批量添加
			List<LogOperationDetail> list = vo.getObjs();
			if (!Validator.isEmpty(list)) {
				MethodParam param = new MethodParam("Batch", "", "", entityName);
				param.setVaule(list);
				if (logOperationDetailDao.insertBath(param)) {
					return "true";
				}
			}
		} else {//添加单个对象
			String infoId = Validator.generate();
			//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
			String cacheKey = "";
			//String cacheKey="logOperationDetail_"+infoId;
			logOperationDetail.setDetailId(infoId);
			MethodParam param = new MethodParam("LogOperationDetail", cacheKey, "", entityName);
			param.setVaule(logOperationDetail);
			if (logOperationDetailDao.insert(param)) {
				return infoId;
			}
		}
		return "";
	}

	public boolean editInfo(ParamsVo<LogOperationDetail> vo) throws BusinessException {
		LogOperationDetail logOperationDetail = vo.getObj();
		if (Validator.isNotNull(logOperationDetail.getDetailId())) {
			String cacheKey = "";
			//String cacheKey="logOperationDetail_"+logOperationDetail.getDetailId();
			MethodParam param = new MethodParam("LogOperationDetail", cacheKey, "", entityName);
			param.setVaule(logOperationDetail);
			return logOperationDetailDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<LogOperationDetail> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="logOperationDetail_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		LogOperationDetail info = (LogOperationDetail) logOperationDetailDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("LogOperationDetail");
			return logOperationDetailDao.edit(param);
		} else {
			param.setParams("detailId", infoId);
			param.setDelete(true);
			return logOperationDetailDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<LogOperationDetail> vo) {
		String key = Validator.getDefaultStr(vo.getMethodKey(), "ById");
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="logOperationDetail_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam(key, cacheKey, "", entityName);
		param.setInfoId(infoId);
		return logOperationDetailDao.selectById(param);
	}

	public Object execute(ParamsVo<LogOperationDetail> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<LogOperationDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = logOperationDetailDao.pageSelect(param);
		return page;
	}

	public List<LogOperationDetail> getList(ParamsVo<LogOperationDetail> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return logOperationDetailDao.select(param);
	}

	public long countInfo(ParamsVo<LogOperationDetail> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return logOperationDetailDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<LogOperationDetail> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(logOperationDetailDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		LogOperationDetail logOperationDetail = vo.getObj();
		if (!Validator.isEmpty(logOperationDetail)) {
			if (Validator.isNotNull(logOperationDetail.getLogId())) {
				sb.append(" and logId = :logId");
				param.setParams("logId", logOperationDetail.getLogId());
			}
			if (Validator.isNotNull(logOperationDetail.getPropertyValue())) {
				sb.append(" and propertyValue = :propertyValue");
				param.setParams("propertyValue", logOperationDetail.getPropertyValue());
			}
			if (Validator.isNotNull(logOperationDetail.getPropertyValue())) {
				sb.append(" and propertyEname = :propertyEname");
				param.setParams("propertyEname", logOperationDetail.getPropertyEname());
			}
			String primaryKey = "000";
			if (Validator.isNotNull(logOperationDetail.getPrimaryKey())) {
				primaryKey = logOperationDetail.getPrimaryKey();
			}
			sb.append(" and primaryKey = :primaryKey");
			//param.setParams("primaryKey", primaryKey);
		} else {
			sb.append(" and primaryKey = :primaryKey");
			param.setParams("primaryKey", "000");
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
